import config from '../config/b.core.js' // 域名配置项
const headerConfig = {
  "Content-Type": "application/json"
}
export function request(url, options = {}) {
  return new Promise((resolve, reject) => {
    var params = {};
    var token = wx.getStorageSync('token');
    
    if (token && token !== '') {
      params.userToken = token;
    }

    params = Object.assign(params, options);
    wx.request({
      url: config.serverUrl + url,
      data: params,
      header: headerConfig,
      method: 'POST',
      success: function (res) {
        if (res.statusCode === 200) {
          if (res.data.errCode === '200003') { // 登录过期
            reLogin().then(() => {
              // console.log(url, options)
              request(url, options).then(res => {
                resolve(res)
              })
            })
          } else if (res.data.errCode == '600000') { // 内部错误
            wx.showToast({
              // title: '网络错误',
              title: '系统内部错误',
              icon: 'none',
              duration: 1500
            })
            reject(res)
          } else {
            resolve(res)
          }
        } else {
          reject(res)
        }
      },
      fail: function (res) {
        console.log(res)
        wx.showToast({
          title: '网络错误',
          icon: 'none',
          duration: 1500
        })
        reject(res)
      },
      complete: function (res) { }
    })
  })
}
// 重新登录
export function reLogin() {
  return new Promise((resolve, reject) => {
    var that = this
    // 登录
    wx.login({
      success: resp => {
        // 发送 resp.code 到后台换取 openId, sessionKey, unionId
        // console.log(resp);
        wx.getUserInfo({
          withCredentials: true,
          success: userInfoRes => {
            console.log(userInfoRes)
            var params = {
              wxCode: resp.code,
              wxEncryptedData: userInfoRes.encryptedData || '',
              iv: userInfoRes.iv || ''
            }
            wx.request({
              url: config.serverUrl + 'user/v1/login',
              data: params,
              header: { "Content-Type": "application/json" },
              method: 'POST',
              responseType: 'text',
              success: function (data) {
                // console.log(data)
                if (data.data.errCode == '0') {
                  wx.setStorageSync('token', data.data.data.userToken)
                  let storageTalentId = wx.getStorageSync('talentId')
                  if (data.data.data.talentId && !storageTalentId) {
                    wx.setStorageSync('talentId', data.data.data.talentId)
                  }
                  resolve()
                } else {
                  wx.showToast({
                    title: data.data.msg,
                    icon: 'none',
                    duration: 1500
                  })
                  reject()
                }
              },
              fail: function (res) {
                wx.showToast({
                  title: '网络错误',
                  icon: 'none',
                  duration: 1500
                })
                reject()
              },
              complete: function (res) { },
            })
          }
        })
      }
    })
  })
}
